package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author: 冯子靖
 * @Description:
 * @Date: 2022/8/1 9:04
 */
@RestController
@RequestMapping("shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    /**
     * 创建订单
     *
     * @param createOrderRequest
     * @return
     */
    @PostMapping("order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest createOrderRequest, HttpServletRequest servletRequest) {

        String userInfo = (String) servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long uid = Long.parseLong(jsonObject.get("uid").toString());
        createOrderRequest.setUserId(uid);

        //设置唯一键
        createOrderRequest.setUniqueKey(UUID.randomUUID().toString());

        //业务主逻辑
        CreateOrderResponse response = orderCoreService.createOrder(createOrderRequest);

        if (SysRetCodeConstants.SUCCESS.getCode().equals(response.getCode())) {
            return new ResponseUtil<String>().setData(response.getOrderId(), response.getMsg());

        }

        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }

    /**
     * 订单列表
     *
     * @param orderListRequest
     * @return
     */
    @GetMapping("order")
    public ResponseData printOrderList(OrderListRequest orderListRequest, HttpServletRequest servletRequest) {

        String userInfo = (String) servletRequest.getHeader("user_info");
        JSONObject jsonObject = JSON.parseObject(userInfo);
        long uid = Long.parseLong(jsonObject.get("uid").toString());
        orderListRequest.setUserId(uid);

        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);
        return new ResponseUtil<>().setData(orderListResponse, "success");
    }

    /**
     * 查询订单详情
     * @param servletRequest
     * @return
     */
    @GetMapping("order/**")
    public ResponseData printOrderInfo(HttpServletRequest servletRequest) {
        String requestURI = servletRequest.getRequestURI();
        String[] split = requestURI.split("/");
        String orderId = split[split.length - 1];

        OrderDetailRequest request = new OrderDetailRequest();
        request.setOrderId(orderId);

        OrderDetail orderDetail = orderQueryService.orderInfo(request);

        return new ResponseUtil<>().setData(orderDetail, "success");
    }

    /**
     * 取消订单
     */
    @PostMapping("cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderRequest request) {
        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(request);

        return new ResponseUtil<>().setData(cancelOrderResponse, "success");
    }

    /**
     * 删除订单
     */
    @DeleteMapping("order/**")
    public ResponseData deleteOrder(HttpServletRequest servletRequest) {
        String requestURI = servletRequest.getRequestURI();
        String[] split = requestURI.split("/");
        String orderId = split[split.length - 1];

        DeleteOrderRequest request = new DeleteOrderRequest();
        request.setOrderId(orderId);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(request);
        if (deleteOrderResponse.getCode().equals(OrderRetCode.ORDER_DEL_ERROR.getCode())) {
           return new ResponseUtil<>().setErrorMsg(deleteOrderResponse.getMsg());
        }
        return new ResponseUtil<>().setData(deleteOrderResponse);
    }
}
